const utils = require('j7/utils');
const events = {};

async function internalAdd(clsName, modName) {
  const modClass = require('./' + modName);
  events[clsName] = {
    'clsName': clsName,
    'modName': modName,
    'class': modClass,
  };
  if (modClass.staticInit) {
    await modClass.staticInit();
  }
}

async function add(clsName, modName) {
  await internalAdd(clsName, modName);
}

async function init() {
  await add('CFHero.Transfer', 'CFHero/transfer');
  await add('CFHero_normal.Transfer', 'CFHero_normal/transfer');
  await add('GoldBrick.Transfer', 'GoldBrick/transfer');
  await add('FounderTag.Transfer', 'FounderTag/transfer');
  await add('NFTLock.Lock', 'NFTLock/lock');
}

function getEventHandle(eventProc) {
  const clsName = eventProc.getContractName() + '.' + eventProc.getEventName();
  return utils.hasKey(events, clsName) ? events[clsName] : null;
}

function createEventHandle(eventProc, conn, eventDb) {
  const clsName = eventProc.getContractName() + '.' + eventProc.getEventName();
  return new events[clsName]['class'](eventProc, conn, eventDb);
}

exports.init = init;
exports.getEventHandle = getEventHandle;
exports.createEventHandle = createEventHandle;
